﻿using DotNetMVC.Common;
using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Website.Areas.WebAdmin.Common;
using DotNetMVC.Website.Areas.WebAdmin.Filters;
using DotNetMVC.Website.Areas.WebAdmin.Models;
using DotNetMVC.Website.Areas.WebAdmin.Models.StaticPage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.Website.Areas.WebAdmin.Controllers
{
    [AdminAuthorize(IsCheck = true)]
    public class SiteNavController : Controller
    {
        public IFunctionalModuleService functionalModuleService { get; set; }
        public ISiteNavService siteNavService { get; set; }
        public ISiteContentService siteContentService { get; set; }

        // GET: WebAdmin/SiteNav
        [HttpGet]
        public ActionResult List()
        {
            List<SiteNavDTO> model = siteNavService.GetSiteNavsByLevel();
            return View(model);
        }

        [HttpGet]
        public ActionResult Add()
        {
            SiteNavAddGetViewModel model = new SiteNavAddGetViewModel();

            #region 功能分类
            List<SelectListItem> listFun = new List<SelectListItem>();
            foreach (FunctionalModuleDTO dto in functionalModuleService.GetEnabledFunctionalModules())
            {
                SelectListItem listItem = new SelectListItem();
                listItem.Text = dto.FunName;
                listItem.Value = dto.FunCode;
                listFun.Add(listItem);
            }
            model.ListFun = listFun;
            #endregion

            #region 上级菜单
            List<SelectListItem> listSiteNav = new List<SelectListItem>();
            listSiteNav.Add(new SelectListItem { Value = "0", Text = "一级菜单" });
            foreach (SiteNavDTO dto in siteNavService.GetSiteNavsByLevel(1))
            {
                SelectListItem listItem = new SelectListItem();
                for (int i = 0; i < dto.NavLevel; i++)
                {
                    dto.NavName = "|-" + dto.NavName;
                }
                listItem.Text = dto.NavName;
                listItem.Value = dto.Id.ToString();
                listItem.Selected = false;
                listSiteNav.Add(listItem);
            }
            model.ListSiteNav = listSiteNav;
            #endregion

            #region 打开方式
            List<SelectListItem> listNavTarget = new List<SelectListItem>();
            listNavTarget.Add(new SelectListItem { Value = "_self", Text = "原窗口打开" });
            listNavTarget.Add(new SelectListItem { Value = "_blank", Text = "弹出新窗口" });
            model.ListNavTarget = listNavTarget;
            #endregion

            #region 是否显示
            List<SelectListItem> listIsShow = new List<SelectListItem>();
            listIsShow.Add(new SelectListItem { Value = "1", Text = "是" });
            listIsShow.Add(new SelectListItem { Value = "0", Text = "否" });
            model.ListIsShow = listIsShow;
            #endregion

            return View(model);
        }

        [HttpPost]
        public ActionResult Add(string functionalModule, long parentNav, string navName, string navUrl, string navTarget, int isShow, string navImg, int sort)
        {
            if (string.IsNullOrEmpty(navName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "菜单名称不能为空" });
            }

            FunctionalModuleDTO modelFunctionalModule = functionalModuleService.GetDTOByFunCode(functionalModule);
            if (modelFunctionalModule == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到功能分类信息" });
            }

            SiteNavDTO modelParentNav = siteNavService.GetDTOById(parentNav);
            if (parentNav > 0 && modelFunctionalModule == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到上级菜单信息" });
            }

            long navId = siteNavService.Add(modelFunctionalModule, modelParentNav, navName, navUrl, navTarget, isShow == 1, navImg, sort).Id;

            //清除头部缓存
            StaticPageHelper.ClearHeaderCache(ControllerContext);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "新增菜单：" + navName + "（Id:" + navId + "）");

            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpGet]
        public ActionResult Edit(long id)
        {
            SiteNavEditGetViewModel model = new SiteNavEditGetViewModel();

            SiteNavDTO dtoSiteNav = siteNavService.GetDTOById(id);
            if (dtoSiteNav == null)
            {
                return View("Error", (object)"获取不到菜单信息");
            }
            model.SiteNav = dtoSiteNav;

            #region 功能分类
            List<SelectListItem> listFun = new List<SelectListItem>();
            foreach (FunctionalModuleDTO dto in functionalModuleService.GetEnabledFunctionalModules())
            {
                SelectListItem listItem = new SelectListItem();
                listItem.Text = dto.FunName;
                listItem.Value = dto.FunCode;
                listFun.Add(listItem);
            }
            model.ListFun = new SelectList(listFun, "Value", "Text", dtoSiteNav.FunCode);
            #endregion

            #region 上级菜单
            List<SelectListItem> listSiteNav = new List<SelectListItem>();
            listSiteNav.Add(new SelectListItem { Value = "0", Text = "一级菜单" });
            foreach (SiteNavDTO dto in siteNavService.GetSiteNavsByLevel(1))
            {
                SelectListItem listItem = new SelectListItem();
                for (int i = 0; i < dto.NavLevel; i++)
                {
                    dto.NavName = "|-" + dto.NavName;
                }
                listItem.Text = dto.NavName;
                listItem.Value = dto.Id.ToString();
                listItem.Selected = false;
                listSiteNav.Add(listItem);
            }
            model.ListSiteNav = new SelectList(listSiteNav, "Value", "Text", dtoSiteNav.ParentId);
            #endregion

            #region 打开方式
            List<SelectListItem> listNavTarget = new List<SelectListItem>();
            listNavTarget.Add(new SelectListItem { Value = "_self", Text = "原窗口打开" });
            listNavTarget.Add(new SelectListItem { Value = "_blank", Text = "弹出新窗口" });
            model.ListNavTarget = new SelectList(listNavTarget, "Value", "Text", dtoSiteNav.NavTarget);
            #endregion

            #region 是否显示
            List<SelectListItem> listIsShow = new List<SelectListItem>();
            listIsShow.Add(new SelectListItem { Value = "1", Text = "是" });
            listIsShow.Add(new SelectListItem { Value = "0", Text = "否" });
            model.ListIsShow = new SelectList(listIsShow, "Value", "Text", dtoSiteNav.IsShow ? 1 : 0);
            #endregion

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(long id, string functionalModule, long parentNav, string navName, string navUrl, string navTarget, int isShow, string navImg, int sort)
        {
            if (string.IsNullOrEmpty(navName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "菜单名称不能为空" });
            }

            FunctionalModuleDTO modelFunctionalModule = functionalModuleService.GetDTOByFunCode(functionalModule);
            if (modelFunctionalModule == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到功能分类信息" });
            }

            SiteNavDTO modelParentNav = siteNavService.GetDTOById(parentNav);
            if (parentNav > 0 && modelFunctionalModule == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到上级菜单信息" });
            }

            siteNavService.Update(id, modelFunctionalModule, modelParentNav, navName, navUrl, navTarget, isShow == 1, navImg, sort);

            //清除头部缓存
            StaticPageHelper.ClearHeaderCache(ControllerContext);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "修改菜单：" + navName + "（Id:" + id + "）");

            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpPost]
        public ActionResult Delete(long id)
        {
            SiteNavDTO nav = siteNavService.GetDTOById(id);
            if (nav == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "菜单不存在" });
            }
            siteNavService.MarkDeleted(id);

            //清除头部缓存
            StaticPageHelper.ClearHeaderCache(ControllerContext);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "删除菜单" + nav.NavName + "（Id:" + nav.Id + "）");

            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            if (selectdIds.Length <= 0)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未选中删除数据" });
            }

            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            siteNavService.BatchMarkDeleted(list);

            //清除头部缓存
            StaticPageHelper.ClearHeaderCache(ControllerContext);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "批量删除菜单（Ids:" + string.Join(",", list) + "）");

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}